/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.buttonmanager.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButtonGroup;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;

/**
 * ClassName:PlatButtonGroupService <br/>
 * Date: 2015年5月8日 下午4:28:49 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatButtonGroupService")
public class PlatButtonGroupService extends BaseService {
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;

	@Transactional
	public PlatButtonGroup save(PlatButtonGroup button) {
		String id =  button.save();
		button.setBtnGId(id);
		return button;
	}

	@Transactional
	public PlatButtonGroup update(PlatButtonGroup button) {
		return button.update();
	}

	@Transactional
	public PlatButtonGroup delete(PlatButtonGroup button) {
		return button.delete();
	}

	@Transactional
	public PlatButtonGroup saveOrUpdate(PlatButtonGroup button) {
		return button.saveOrUpdate();
	}
	
	/**
	 * 获取按钮组
	 * @param groupId
	 * @return
	 */
	public List getButtons(String btnGId){
		List buttons = new ArrayList();
		
		Cache cache = cacheManager.getCache("ButtonCache");
		
		Query query = cache.createQuery();
		
		Attribute eq = cache.getSearchAttribute("btnGId");
		query.addCriteria(eq.eq(btnGId));
		
		
		query.includeAttribute(eq);
		query.includeValues();
		query.end();
		Results result = query.execute();
		
		for(Result r : result.all()){
			buttons.add(r.getValue());
		}
		return buttons;
	}

}
